package com.ruoyi.generator.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.generator.domain.FromBuildOptionsConfig;
import com.ruoyi.generator.service.IFromBuildConfigService;
import com.ruoyi.generator.service.IFromBuildOptionsConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 单设计_展示单下拉框、单选框、多选框选项值Controller
 *
 * @author XL
 * @date 2022-08-05 12:14:16
 */
@RestController
@RequestMapping("/tool/gen/FromBuildOptionsConfig")
public class FromBuildOptionsConfigController extends BaseController
{
    @Autowired
    private IFromBuildOptionsConfigService fromBuildOptionsConfigService;

    @Autowired
    private IFromBuildConfigService fromBuildConfigService;

    @Autowired
    private TokenService tokenService;

    /**
     * 获取字段信息
     * @return
     */
    @GetMapping("/init")
    public AjaxResult getFieldInfo(){

        return fromBuildConfigService.getColumnConfigByTableName("sys_from_build_options_config");
    }

    /**
     * 查询单设计_展示单下拉框、单选框、多选框选项值列表
     */
    @PreAuthorize("@ss.hasPermi('system:FromBuildOptionsConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(FromBuildOptionsConfig fromBuildOptionsConfig){
        try{
            startPage();
            List<FromBuildOptionsConfig> list = fromBuildOptionsConfigService.selectFromBuildOptionsConfigList(fromBuildOptionsConfig);
            return getDataTable(list);
        }catch (Exception e){
            logger.error("FromBuildOptionsConfigController接口list异常",e);
            return null;
        }
    }

    /**
     * 导出单设计_展示单下拉框、单选框、多选框选项值列表
     */
    @PreAuthorize("@ss.hasPermi('system:FromBuildOptionsConfig:export')")
    @Log(tableName = "sys_from_build_options_config", title = "单设计_展示单下拉框、单选框、多选框选项值", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FromBuildOptionsConfig fromBuildOptionsConfig){
        try{
            List<FromBuildOptionsConfig> list = fromBuildOptionsConfigService.selectFromBuildOptionsConfigList(fromBuildOptionsConfig);
            ExcelUtil<FromBuildOptionsConfig> util = new ExcelUtil<FromBuildOptionsConfig>(FromBuildOptionsConfig.class);
            return util.exportExcel(list, "FromBuildOptionsConfig");
        }catch (Exception e){
            logger.error("FromBuildOptionsConfigController接口export异常",e);
            return AjaxResult.error("操作失败");
        }
    }

    /**
     * 导入单设计_展示单下拉框、单选框、多选框选项值列表
     */

    /**
     * 获取单设计_展示单下拉框、单选框、多选框选项值详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:FromBuildOptionsConfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        try{
            return AjaxResult.success(fromBuildOptionsConfigService.selectFromBuildOptionsConfigById(id));
        }catch (Exception e){
            logger.error("FromBuildOptionsConfigController接口getInfo异常",e);
            return AjaxResult.error("获取异常");
        }
    }

    /**
     * 新增单设计_展示单下拉框、单选框、多选框选项值
     */
    @PreAuthorize("@ss.hasPermi('system:FromBuildOptionsConfig:add')")
    @Log(tableName = "sys_from_build_options_config", title = "单设计_展示单下拉框、单选框、多选框选项值", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FromBuildOptionsConfig fromBuildOptionsConfig){
        try{
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            fromBuildOptionsConfig.setCreateBy(loginUser.getUsername());
            fromBuildOptionsConfig.setCreateTime(new Date());
            return toAjax(fromBuildOptionsConfigService.insertFromBuildOptionsConfig(fromBuildOptionsConfig));
        }catch (Exception e){
            logger.error("FromBuildOptionsConfigController接口add异常",e);
            return AjaxResult.error("操作失败");
        }
    }

    /**
     * 修改单设计_展示单下拉框、单选框、多选框选项值
     */
    @PreAuthorize("@ss.hasPermi('system:FromBuildOptionsConfig:edit')")
    @Log(tableName = "sys_from_build_options_config", title = "单设计_展示单下拉框、单选框、多选框选项值", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FromBuildOptionsConfig fromBuildOptionsConfig){
        try{
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            fromBuildOptionsConfig.setUpdateBy(loginUser.getUsername());
            fromBuildOptionsConfig.setUpdateTime(new Date());
            return toAjax(fromBuildOptionsConfigService.updateFromBuildOptionsConfig(fromBuildOptionsConfig));
        }catch (Exception e){
            logger.error("FromBuildOptionsConfigController接口edit异常",e);
            return AjaxResult.error("操作失败");
        }
    }

    /**
     * 删除单设计_展示单下拉框、单选框、多选框选项值
     */
    @PreAuthorize("@ss.hasPermi('system:FromBuildOptionsConfig:remove')")
    @Log(tableName = "sys_from_build_options_config", title = "单设计_展示单下拉框、单选框、多选框选项值", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        try{
            return toAjax(fromBuildOptionsConfigService.deleteFromBuildOptionsConfigByIds(ids));
        }catch (Exception e){
            logger.error("FromBuildOptionsConfigController接口remove异常",e);
            return AjaxResult.error("操作失败");
        }
    }

    @Log(tableName = "sys_from_build_options_config", title = "单设计_展示单下拉框、单选框、多选框选项值", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:FromBuildOptionsConfig:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<FromBuildOptionsConfig> util = new ExcelUtil<FromBuildOptionsConfig>(FromBuildOptionsConfig.class);
        List<FromBuildOptionsConfig> dataList = util.importExcel(file.getInputStream());
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        String operName = loginUser.getUsername();
        int successCount=0;
        for(FromBuildOptionsConfig item : dataList){
            successCount+=fromBuildOptionsConfigService.insertFromBuildOptionsConfig(item);
        }
        String result= StringUtils.format("导入成功条数:{}，失败条数：{}",successCount,(dataList.size()-successCount));
        return AjaxResult.success(result);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<FromBuildOptionsConfig> util = new ExcelUtil<FromBuildOptionsConfig>(FromBuildOptionsConfig.class);
        return util.importTemplateExcel("单设计_展示单下拉框、单选框、多选框选项值");
    }

}
